/*
 * Licensed Materials - Property of tenxcloud.com
 * (C) Copyright 2023 TenxCloud. All Rights Reserved.
 * 2023-04-08  @author xyinting
 */

package material

import (
	"github.com/jinzhu/gorm"
	"time"
)

type FileResponse struct {
	Href      string     `json:"href"`       // 文件的路径
	UriPrefix string     `json:"uri_prefix"` // 文件服务的前缀， 一版为 '/'
	HostAddr  string     `json:"host_addr"`  // 文件服务的URL
	Paths     []FileInfo `json:"paths"`
	Total     int        `json:"total"`
}

type FileInfo struct {
	PathType string `json:"path_type"`
	Name     string `json:"name"`
	Time     int64  `json:"mtime"`
	Size     int64  `json:"size"`
}

// CREATE TABLE `material` (
//
//	`id` int NOT NULL AUTO_INCREMENT,
//	`cat_id` int DEFAULT NULL,
//	`user_id` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//	`path` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//	`url` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//	`created_at` datetime DEFAULT NULL,
//	`updated_at` datetime DEFAULT NULL,
//	PRIMARY KEY (`id`)
//
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
type Material struct {
	Id        int       `json:"id" gorm:"column:id"`
	CatId     int       `json:"cat_id" gorm:"column:cat_id"`
	UserId    string    `json:"user_id" gorm:"column:user_id"`
	Path      string    `json:"path" gorm:"column:path"`
	Url       string    `json:"url" gorm:"column:url"`
	CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
}

func (m *Material) TableName() string {
	return "material"
}
func (m *Material) Create(db *gorm.DB) error {
	return db.Create(m).Error
}
func (m *Material) Update(db *gorm.DB) error {
	return db.Save(m).Error
}
func (m *Material) GetById(db *gorm.DB) error {
	return db.Where("id = ?", m.Id).First(m).Error
}
func (m *Material) GetAll(db *gorm.DB) ([]Material, error) {
	var materials []Material
	return materials, db.Where("user_id = ?", m.UserId).Find(&materials).Error
}
func (m *Material) UpdatePartFiles(db *gorm.DB, files map[string]interface{}) error {
	return db.Model(m).Where("id = ?", m.Id).Update(files).Error
}
func (m *Material) GetByCategory(db *gorm.DB) ([]Material, error) {
	var materials []Material
	return materials, db.Where("user_id = ? and cat_id = ?", m.UserId, m.CatId).Find(&materials).Error
}

// CREATE TABLE `material_categories` (
//
//	`id` bigint NOT NULL AUTO_INCREMENT,
//	`user_id` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//	`category` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//	`created_at` datetime DEFAULT NULL,
//	`updated_at` datetime DEFAULT NULL,
//	`status` tinyint(1) DEFAULT '1',
//
// `img` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//
//	PRIMARY KEY (`id`)
//
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
type MaterialCategories struct {
	Id        int       `json:"id" gorm:"column:id"`
	UserId    string    `json:"user_id" gorm:"column:user_id"`
	Category  string    `json:"category" gorm:"column:category"`
	CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
	Status    int       `json:"status" gorm:"column:status"`
	Img       string    `json:"img" gorm:"column:img"`
}

func (m *MaterialCategories) TableName() string {
	return "material_categories"
}

func (m *MaterialCategories) Create(db *gorm.DB) error {
	return db.Create(m).Error
}

func (m *MaterialCategories) Update(db *gorm.DB) error {
	return db.Save(m).Error
}
func (m *MaterialCategories) GetById(db *gorm.DB) error {
	return db.Where("id = ?", m.Id).First(m).Error
}
func (m *MaterialCategories) GetAll(db *gorm.DB) ([]MaterialCategories, error) {
	var categories []MaterialCategories
	return categories, db.Where("user_id = ?", m.UserId).Find(&categories).Error
}
func (m *MaterialCategories) UpdatePartFiles(db *gorm.DB, files map[string]interface{}) error {
	return db.Model(m).Where("id = ?", m.Id).Update(files).Error
}
func (m *MaterialCategories) CountByUserId(db *gorm.DB) (int, error) {
	count := 0
	return count, db.Model(m).Where("user_id = ?", m.UserId).Count(&count).Error
}
